//
//  main.m
//  05-集合遍历-数组的排序
//
//  Created by dllo on 16/7/8.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[]) {
    // 数组遍历 for循环
    NSArray *arr = @[@"上海", @"天津", @"沈阳", @"抚顺", @"朝阳"];
    NSMutableArray *newArr = [NSMutableArray array];
    for (int i = 4; i >= 0; i--) {
        [newArr addObject:arr[i]];
    }
    NSLog(@"新数组:%@", newArr);
    
    NSDictionary *dic = @{@"name": @"刘阳", @"sex": @"男", @"age": @"23"};
    NSArray *allkeys = dic.allKeys;
    for (int i = 0; i < allkeys.count; i++) {
        NSString *key = allkeys[i];
        NSString *string = [dic objectForKey:key];
        NSLog(@"详细的信息为:%@", string);
    }
    // 枚举器(遍历方式)
    // 数组的枚举器
    // NSEnumerator 依赖于集合类型(数组,字典,集合)不能单独使用
    NSEnumerator *stringArrayEnumerator = [arr objectEnumerator];
    id value = nil;
    while (value = [stringArrayEnumerator nextObject]) {
        NSLog(@"结果1:%@", value);
    }
    // 反向枚举器
    NSEnumerator *reverse = [arr reverseObjectEnumerator];
    id value1 = nil;
    while (value1 = [reverse nextObject]) {
        NSLog(@"结果2:%@", value1);
    }
    // 字典的枚举器(字典无须 所以没有倒序)
    // 字典枚举器遍历的默认值是value
    NSEnumerator *dicEnumerator = [dic.allKeys objectEnumerator];
    id value2 = nil;
    while (value2 = [dicEnumerator nextObject]) {
        NSLog(@"结果3:%@", value2);
    }
    // 集合枚举器(无序,没有倒序枚举)
    NSSet *set = [NSSet setWithObjects:@"shanghai", @"tianjin", @"beijing", @"shenyang", nil];
    
    NSEnumerator *setEnumerator = [set objectEnumerator];
    id value3 = nil;
    while (value3 = [setEnumerator nextObject]) {
        NSLog(@"结果4:%@", value3);
    }
    // 快速枚举 for...in
    // 快速枚举一个数组
    NSString *appendstr = [NSString string];
    for (NSString *string in [arr reverseObjectEnumerator]) {
        appendstr = [appendstr stringByAppendingString:string];
    }
    NSLog(@"结果5:%@", appendstr);
    // 快速枚举一个字典
    // 默认值为key值
    for (NSString *key in dic) {
        NSLog(@"结果6:%@", key);
        NSLog(@"结果7:%@", [dic objectForKey:key]);
    }
    // 快速枚举一个集合
    for (NSString *str in set) {
        NSLog(@"结果8:%@", str);
    }
    // 排序
    // 排序: 可变数组(sortUsingDescriptors)和不可变数组(sortedArrayUsingDescriptors)通过sort排序调用的方法不同
    NSArray *array5 = @[@"1", @"3", @"5", @"2", @"0"];
    NSMutableArray *array6 = [NSMutableArray arrayWithObjects:@56, @23, @10, @102, @56, nil];
    NSSortDescriptor * sort = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES];
    // 可以使用多个排序方法,先遵循第一个排序方法,再遵循后面的方法
    array5 = [array5 sortedArrayUsingDescriptors:@[sort]];
    [array6 sortUsingDescriptors:@[sort]];
    NSLog(@"结果9:%@", array5);
    NSLog(@"结果10:%@", array6);
    // 创建3个联系人
    Person *person1 = [[Person alloc] init];
    person1.name = @"xiening";
    person1.age = 20;
    Person *person2 = [[Person alloc] init];
    person2.name = @"liuyang";
    person2.age = 18;
    Person *person3 = [[Person alloc] init];
    person3.name = @"shengli";
    person3.age = 25;
    NSArray *arrOfName = @[person1, person2, person3];
    NSSortDescriptor *sortName = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    NSSortDescriptor *sortAge = [[NSSortDescriptor alloc] initWithKey:@"age" ascending:YES];
    NSArray *arrOfNewName = [arrOfName sortedArrayUsingDescriptors:@[sortName]];
    NSArray *arrOfNewAge = [arrOfName sortedArrayUsingDescriptors:@[sortAge]];
    NSLog(@"结果11:%@", arrOfNewName);
    NSLog(@"结果12:%@", arrOfNewAge);
    //排序2
    arrOfName = [arrOfName sortedArrayUsingSelector:@selector(compareByName:)];
    NSLog(@"结果13:%@", arrOfName);
    NSArray *sortString = @[@"iphone", @"iPad", @"iTouch", @"iMac", @"Mac MiNi"];
    NSLog(@"结果14:%@", sortString);
    sortString = [sortString sortedArrayUsingSelector:@selector(compare:)];
    NSLog(@"结果15:%@", sortString);
    
       return 0;
}
